package com.zzq.tools.datasource;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;

public class MySqlCompareDataSource implements CompareDataSource {

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private final DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    private MySqlCompareDataSource(MySqlCompareDataSourceBuilder mySqlCompareDataSourceBuilder) {
        this.dataSource = mySqlCompareDataSourceBuilder.dataSource;
    }

    public static class MySqlCompareDataSourceBuilder {
        private DataSource dataSource;
        private String url;
        private String user;
        private String password;

        public MySqlCompareDataSourceBuilder() {
            super();
        }

        public MySqlCompareDataSourceBuilder url(String url) {
            this.url = url;
            return this;
        }
        public MySqlCompareDataSourceBuilder user(String user) {
            this.user = user;
            return this;
        }
        public MySqlCompareDataSourceBuilder password(String password) {
            this.password = password;
            return this;
        }

        public MySqlCompareDataSource build() {
            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUrl(this.url);
            dataSource.setUser(this.user);
            dataSource.setPassword(this.password);
            this.dataSource = dataSource;
            return new MySqlCompareDataSource(this);
        }
    }

}
